<%@ page import="com.devicemantra.coordinate.dataobjects.*,java.util.*,com.google.appengine.api.datastore.KeyFactory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/css/tab.css" />
<script type="text/javascript">
function initXMLHttpClient() 
{
     var xmlhttp;
     try {
         // Mozilla / Safari / IE7
         xmlhttp = new XMLHttpRequest();
     } 
     catch (e) 
     {
          // IE
          var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
                                     'MSXML2.XMLHTTP.4.0',
                                     'MSXML2.XMLHTTP.3.0',
                                     'MSXML2.XMLHTTP',
                                     'Microsoft.XMLHTTP' );
        var success = false;
          for (var i=0;i < XMLHTTP_IDS.length && !success; i++) 
	  {
              try 
	    	{
                   xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
                      success = true;
                } catch (e) {}
          }
          if (!success) 
	  {
              throw new Error('Unable to create XMLHttpRequest.');
          }
      }
     return xmlhttp;
}

function button_clicked()
{
	var lSubDeptName = document.getElementById("new_dept").value;
	var lSelectBox = document.getElementById("select_box");
	var lDeptName = lSelectBox.options[lSelectBox.selectedIndex].text;
	//alert(lSubDeptName + " - " + lDeptName);
	var httpGetReq = initXMLHttpClient();
	httpGetReq.onreadystatechange =function() 
	{
		if(httpGetReq.readyState == 4)
   		{
        		if (httpGetReq.status==200)
        		{
          		//alert(httpGetReq.responseText);
          		//document.getElementById('anim_window').innerHTML = "";
          		document.getElementById('show_window').innerHTML = httpGetReq.responseText;
			}
		}
	}
	
	//alert("GET'ing getWindow.jsp?category=subdepartment&department=" + lSelectBox.options[lSelectBox.selectedIndex].text);
	var lUrl = "/AddSubDepartment?dept_name=" + lDeptName + "&sub_dept_name=" + lSubDeptName;
	//alert(lUrl);
	httpGetReq.open("GET","/AddSubDepartment?dept_name=" + lDeptName + "&sub_dept_name=" + lSubDeptName,true);
	httpGetReq.send(null);
	
}

function change(checkedValue)
{
	//alert(checkedValue);
	
	if( checkedValue == "add")
	{
		document.getElementById('temp').innerHTML = "<br> Enter name of new Sub Department: <br><input type=text id=new_dept value=\"Enter Name\"";
	}
	else
	{
		document.getElementById('temp').innerHTML = "<br> Enter delete value <br>";
	}
			
}

function js_getsubdepts(selection)
{
	var lSelectBox = selection;	
	//alert("Selection was " + lSelectBox.options[lSelectBox.selectedIndex].text);
	document.getElementById('temp').innerHTML = "";
	document.getElementById('show_window').innerHTML = "";
	document.getElementById('show_window').innerHTML = "<img src=\"images/animation.gif\" alt=\"Processing...\" />";
	var httpGetReq = initXMLHttpClient();
	httpGetReq.onreadystatechange =function() 
	{
		if(httpGetReq.readyState == 4)
   		{
        		if (httpGetReq.status==200)
        		{
          		//alert(httpGetReq.responseText);
          		//document.getElementById('anim_window').innerHTML = "";
          		document.getElementById('show_window').innerHTML = httpGetReq.responseText;
			}
		}
	}
	
	//alert("GET'ing getWindow.jsp?category=subdepartment&department=" + lSelectBox.options[lSelectBox.selectedIndex].text);
	httpGetReq.open("GET","/modules/administration/getWindow.jsp?category=subdepartment&department=" + lSelectBox.options[lSelectBox.selectedIndex].text,true);
	httpGetReq.send(null);
}
</script>
</head>
<body>
<ul id="tabmenu">
	<li><a href="/AdminController?id=country">Country</a></li>

	<li><a href="/AdminController?id=states">States</a></li>
	<li><a href="/AdminController?id=department">Department</a></li>
	<li><a href="/AdminController?id=users">Users</a></li>
</ul>
<div id="content">
<%
	List<CDepartment> lDeptList = CDepartmentFactory.getAllDepartments();
	System.out.println("List size isS " + lDeptList.size());
	int lSize = lDeptList.size();
	
	if( lSize != 0)
	{
		out.println("<select name=\"dept_name\" id=select_box onchange=\"js_getsubdepts(this)\">");
		out.print("<option>Select a Department</option>");
		for(int counter = 0; counter < lDeptList.size(); counter++)
		{
				out.print("<option value=\"" + KeyFactory.keyToString(lDeptList.get(counter).getDepartmentId()) + "\"");
				out.print(">" + lDeptList.get(counter).getDepartmentName() + "</option>"); 
		}
	}
	else
	{
		out.println("<select name=\"dept_name\" >");
		out.println("<option selected=\"true\">NO DEPARTMENTS</option>");
	}
	out.println("</select>");
	out.print("<div id=\"anim_window\"></div>"); 
	out.println("<BR><BR><BR>");
	out.println("<div id=\"show_window\"></div>");
	out.print("<div id=\"temp\">" + "</div>");
	
	out.println("<BR><BR><BR> <INPUT TYPE = radio name=\"operation\" value=\"add\" onClick=\"change(this.value)\"> Add "  
			+ "   <INPUT TYPE = radio name=\"operation\" value=\"del\" onClick=\"change(this.value)\" > Edit " + 
			"<BR>" +  "<INPUT TYPE = SUBMIT name=\"button1\" onClick=\"button_clicked()\" VALUE=\"Save\">" + 
	"</form>");
%>
</div>
</body>
</html>